//package com.woniu.config;
//
//import com.alibaba.druid.pool.DruidDataSource;
//import com.woniu.filter.CORSAuthenticationFilter;
//import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
//import org.apache.shiro.mgt.SecurityManager;
//import org.apache.shiro.realm.Realm;
//import org.apache.shiro.realm.jdbc.JdbcRealm;
//import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
//import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
//import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import javax.servlet.Filter;
//import javax.sql.DataSource;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * @author 李桢
// * @time 2021/01/16 10:04:38
// */
//
//@Configuration
//public class ShiroConfig {
//
//    @Bean
//    public DataSource dataSource(){
//
//        DruidDataSource dataSource = new DruidDataSource();
//
//        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
//        dataSource.setUrl("jdbc:mysql://172.16.13.90:3306/sell_houses?characterEncoding=utf8&serverTimezone=GMT%2B8");
//        dataSource.setUsername("root");
//        dataSource.setPassword("root");
//
//        return dataSource;
//    }
//
//    @Bean
//    public HashedCredentialsMatcher hcm() {
//        HashedCredentialsMatcher hcm = new HashedCredentialsMatcher();
//        hcm.setHashAlgorithmName("MD5");
//        hcm.setHashIterations(1024);
//        return hcm;
//    }
//
//    @Bean
//    public Realm realm(DataSource dataSource,HashedCredentialsMatcher hcm){
//
//        JdbcRealm realm = new JdbcRealm();
//
//        realm.setDataSource(dataSource);
//
//        // 打招呼，认证查询时，连盐一起查出来。
//        realm.setSaltStyle(JdbcRealm.SaltStyle.COLUMN);
//
//        //加密匹配器
//        realm.setCredentialsMatcher(hcm);
//
//        // 设置认证的sql语句
//        realm.setAuthenticationQuery("select User_Psw,salt from user_infos where User_Name = ?");
//        // 根据用户身份 查询用户拥有的角色
//        realm.setUserRolesQuery("SELECT Roles_Name FROM user_role ur INNER JOIN user_infos u ON ur.`User_ID` = u.`User_ID` INNER JOIN role r ON ur.Rid = r.Rid AND User_Name = ?");
//        // 根据角色名，查询权限
//        realm.setPermissionsQuery("SELECT permissions_Name FROM role_permissions rp INNER JOIN role r ON rp.`User_ID` = r.`User_ID` INNER JOIN permissions p ON rp.`Pid` = p.`Pid` AND Roles_Name = ?");
//        // 以下配置，确保，我们可以根据用户查权限
//        realm.setPermissionsLookupEnabled(true);
//
//        return realm;
//    }
//
//    @Bean
//    public SecurityManager securityManager(Realm realm){
//
//        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//
//        securityManager.setRealm(realm);
//
//
//        return securityManager;
//
//
//    }
//
//    @Bean
//    public ShiroFilterFactoryBean shiroFilterFactory(SecurityManager securityManager){
//
//        ShiroFilterFactoryBean shiroFilterFactory = new ShiroFilterFactoryBean();
//
//        shiroFilterFactory.setSecurityManager(securityManager);
//
////        shiroFilterFactory.setLoginUrl("/view/login");
////        shiroFilterFactory.setUnauthorizedUrl("/view/unauthorized");
//
//        //将默认authc过滤器换为自定义的过滤器
//        Map<String, Filter> filters = shiroFilterFactory.getFilters();
//
//        filters.put("authc",new CORSAuthenticationFilter());
//
//        Map<String,String> configMap = new HashMap<>();
//
//        configMap.put("/**/login/**","anon");
//
//        //登出，自动动出移到了controller处理，放在这处理报策略问题
//        configMap.put("/**/logout","anon");
//
//        //configMap.put("/**/logout/**","logout");
//        configMap.put("/**","authc");
//
//        //给注册用户需要的路径放行，如果后期感觉不合适请联系梁某
//        configMap.put("/**/user/save/**","anon");
//
//
//        shiroFilterFactory.setFilterChainDefinitionMap(configMap);
//
//        return shiroFilterFactory;
//    }
//
//    // 开启shiro注解
//    @Bean
//    public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor(SecurityManager securityManager) {
//        AuthorizationAttributeSourceAdvisor aasa = new AuthorizationAttributeSourceAdvisor();
//        aasa.setSecurityManager(securityManager);
//        return aasa;
//    }
//
//
//
//
//
//}
